package l2hc.gameserver.model.entity;

import java.util.concurrent.CopyOnWriteArrayList;

import l2hc.commons.util.Rnd;
import l2hc.gameserver.Config;
import l2hc.gameserver.dao.AccountBonusDAO;
import l2hc.gameserver.dao.FreePremiumAccountsDao;
import l2hc.gameserver.dao.PremiumAccountRatesHolder;
import l2hc.gameserver.model.Player;
import l2hc.gameserver.network.loginservercon.LoginServerCommunication;
import l2hc.gameserver.network.loginservercon.gspackets.BonusRequest;
import l2hc.gameserver.network.serverpackets.ExBR_PremiumState;
import l2hc.gameserver.network.serverpackets.ExShowScreenMessage;
import l2hc.gameserver.network.serverpackets.L2GameServerPacket;
import l2hc.gameserver.network.serverpackets.MagicSkillUse;
import l2hc.gameserver.scripts.Functions;

public class FreePAManager extends Functions
{
  private static int bonus_group;
private static int time;

@SuppressWarnings({ "rawtypes", "unused" })
public static void checkAndReward(Player player)
  {
    if (player == null) {
      return;
    }

    if ((!FreePremiumAccountsDao.getFreePAList().isEmpty()) && (FreePremiumAccountsDao.getFreePAList() != null))
    {
      for (String account : FreePremiumAccountsDao.getFreePAList())
        if (player.getAccountName().equalsIgnoreCase(account))
        {
          return;
        }
    }
    FreePremiumAccountsDao.addAccount(player.getAccountName());
  
    boolean isHours;

    if (Config.RANDOMIZE_FROM_PA_TABLE)
    {
      CopyOnWriteArrayList all_info = PremiumAccountRatesHolder.getAllAquisions();
      PremiumAccountRatesHolder.PremiumInfo rnd_info = (PremiumAccountRatesHolder.PremiumInfo)all_info.get(Rnd.get(all_info.size() - 1));
      int bonus_group = rnd_info.getGroupNumber();
      int time = rnd_info.getDays();
      isHours = rnd_info.isHours();
    }
    else
    {
      bonus_group = Config.FREE_PA_BONUS_GROUP_STATIC;
      time = Config.FREE_PA_BONUS_TIME_STATIC;
       isHours = Config.FREE_PA_IS_HOURS_STATIC;
    }

    int startTime = (int)(System.currentTimeMillis() / 1000L);
    int bonusExpire;
    if (isHours)
      bonusExpire = startTime + time * 60 * 60;
    else {
       bonusExpire = startTime + time * 24 * 60 * 60;
    }
    switch (Config.SERVICES_RATE_TYPE)
    {
    case 1:
      LoginServerCommunication.getInstance().sendPacket(new BonusRequest(player.getAccountName(), bonus_group, bonusExpire));
      break;
    case 2:
      AccountBonusDAO.getInstance().insert(player.getAccountName(), bonus_group, bonusExpire);
    }

    player.getNetConnection().setBonus(bonus_group);
     player.getNetConnection().setBonusExpire(bonusExpire);

    player.stopBonusTask();
     player.startBonusTask();

    if (player.getParty() != null) {
      player.getParty().recalculatePartyData();
    }
    player.broadcastPacket(new L2GameServerPacket[] { new MagicSkillUse(player, player, 23128, 1, 1, 0L) });
     player.sendPacket(new ExBR_PremiumState(player, true));

    boolean _message = Config.ENABLE_FREE_PA_NOTIFICATION;

    if (_message)
    {
      String delay_type = player.isLangRus() ? "Р”РЅРµР№" : isHours ? "Hours" : player.isLangRus() ? "Р§Р°СЃРѕРІ" : "Days";
      player.sendPacket(new ExShowScreenMessage("Р’Р°Рј Р±С‹Р» РІС‹РґР°РЅ Р±РµСЃРїР»Р°С‚РЅС‹Р№ РїСЂРµРјРёСѓРј Р°РєРєР°СѓРЅС‚ РЅР° " + time + " " + delay_type + "! РџСЂРёСЏС‚РЅРѕР№ РёРіСЂС‹! ", 10000, ExShowScreenMessage.ScreenMessageAlign.TOP_CENTER, true));
    }
  }
}
